import Scanner from "./Scanner"
import nestTokens from "./nestTokens"
/**
 * @description: 解析模板字符串为tokens
 * @param {*} templateStr
 * @return {*}tokens
 */
export default function parseTemplateToTokens(templateStr) {
  const scanner = new Scanner(templateStr)
  const tokens = []
  let words
  while (!scanner.eos()) {
    words = scanner.scanUntil("{{")
    if (words) {
      tokens.push(["text", words.trim()])
    }
    scanner.scan("{{")
    words = scanner.scanUntil("}}")
    if (words) {
      if (words[0] === "#") {
        tokens.push(["#", words.substring(1)])
      } else if (words[0] === "/") {
        tokens.push(["/", words.substring(1)])
      } else {
        tokens.push(["name", words])
      }
    }
    scanner.scan("}}")
  }
  return nestTokens(tokens)
}
